#include "../include/BuyEvent.h"

BuyEvent::BuyEvent() : Request(), _price(0) {}

BuyEvent::BuyEvent(const Poco::DateTime * const  _date, int numUnits, int userId, ass2::eEnergy eType, double price)
	: Request(_date, userId, eType), numOfUnits(numUnits), _price(price){}

BuyEvent::~BuyEvent(){
	delete date;
}

int BuyEvent::getAmount(){
	return numOfUnits;
}

bool BuyEvent::setAmount(int newAmount){
	bool ans = false;
	if (newAmount>numOfUnits | newAmount<=0)
		ans = false;
	else {
		ans = true;
		numOfUnits = newAmount;
	}
	return ans;
}

//returns posotive int if this happens before other;
//negative if other happens before this; 0 otherwise.
int BuyEvent::compareTo(Events & other) const {
	int ans = 0;
	ans = (*(other.date) - *(this->date)).days(); //compare dates
	if (ans == 0) {//dates are the same
		ans = other.getType() - this->type;//compare event type
		if (ans == 0) { //event types are the same
			const BuyEvent &otherBuy = dynamic_cast<BuyEvent&>(other); //cast other to BuyEvent
			ans = otherBuy._energyType - this->_energyType; //compare energy type
			if (ans == 0) { //energy types are the same
				ans = this->_price - otherBuy._price; //compare prices
				if (ans == 0) //price is the same
					ans = otherBuy._userId - this->_userId; //compare user id
			}
		}
	}
	return ans;
}

Events::eEvents BuyEvent::getType() const {
	return type;
}

